<template>
	<!--地址picker-->
	<view :status="checkStatus" v-if="lotusAddressData.visible" class="lotus-address">
		<view class="lotus-address-mask"></view>
		<view :class="lotusAddressData.visible?'lotus-address-box':'lotus-address-box lotus-address-box-out'">
			<view class="lotus-address-action">
				<text @tap="cancelPicker" class="lotus-address-action-cancel">取消</text>
				<text @tap="chosedVal" class="lotus-address-action-affirm">确认</text>
			</view>
			<view class="lotus-address-picker-box">
				<!--省-->
				<scroll-view scroll-y :scroll-into-view="'pid'+pChoseIndex" class="lotus-address-picker-box-item">
					<view @tap="clickPicker(0,pIndex,pItem);" :id="'pid'+pIndex" :class="pIndex === pChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(pItem,pIndex) in province" :key="pIndex">{{pItem.province}}</view>
				</scroll-view>
				<!--市-->
				<scroll-view scroll-y :scroll-into-view="'cid'+cChoseIndex" class="lotus-address-picker-box-item">
					<view @tap="clickPicker(1,cIndex,cItem);" :id="'cid'+cIndex" :class="cIndex === cChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(cItem,cIndex) in city" :key="cIndex">{{cItem.city}}</view>
				</scroll-view>
				<!--区-->
				<scroll-view scroll-y :scroll-into-view="'tid'+tChoseIndex" class="lotus-address-picker-box-item">
					<view @tap="clickPicker(2,tIndex,tItem);" :id="'tid'+tIndex" :class="tIndex === tChoseIndex?'lotus-address-picker lotus-address-picker2':'lotus-address-picker'" v-for="(tItem,tIndex) in town" :key="tIndex">{{tItem.area}}</view>
				</scroll-view>
				<!--区END-->
			</view>
		</view>
	</view>
	<!--地址picker END-->
</template>

<script>
	import {lotusAddressJson} from  "./Winglau14-lotusAddress.js";
	export default {
		props:['lotusAddressData'],
		data() {
			return {
				visible: false,
				province:[],
				city:[],
				town:[],
				provinceName:'',
				cityName:'',
				townName:'',
				type:0,//0新增1编辑
				pChoseIndex:-1,
				cChoseIndex:-1,
				tChoseIndex:-1,
				timeObj:null,
				provinceCode: '',
				cityCode:'',
				townCode:''
			};
		},
		methods:{
			//取消
			cancelPicker(){
				this.visible = false;
				this.$emit("choseVal",{
					isChose:0,
					visible:false
				});
			},
			//获取最后选择的省市区的值
			chosedVal() {
				this.type = 1;
				let isChose = 0;
				//已选省市区 isChose = 1 
				if((this.provinceName&&this.cityName)||(this.provinceName&&this.cityName&&this.townName)){
					isChose = 1;
				}
				this.visible = false;
				this.$emit("choseVal",{
					province:this.provinceName,
					provinceCode:this.provinceCode,
					city:this.cityName,
					cityCode:this.cityCode,
					town:this.townName,
					townCode: this.townCode,
					isChose,
					visible:false
				});
			},
			//获取省市索引值
			getTarId(name,key,parent){
			    let choseIndex = 0;
				if(key === 'province'){
					lotusAddressJson.forEach((item,index)=>{
						// 选择省 parent为空
						if(item.province === name){
						    choseIndex = index
							this.provinceCode = item.code
						}
					});
				}else if(key === 'city'){
					lotusAddressJson[parent].citys.forEach((item,index)=>{
						// 选择市区 需要传父级的choseIndex
						if(item.city === name && parent != undefined){
							choseIndex = index
							this.provinceCode = item.code
						}
					});
				}
			    return choseIndex;
			},
			//获取市数据
			getCityArr(parentId){
			    let city = [];
				if(lotusAddressJson[parentId] && lotusAddressJson[parentId].citys && lotusAddressJson[parentId].citys.length){
					lotusAddressJson[parentId].citys.forEach((item,index)=>{
					    city.push(item);
					});
					return city;
				}
			    return city;
			},
			//获取区数据
			getTownArr(pId,cId){
			    let town = [];
			    lotusAddressJson.forEach((item,index)=>{
			        if(pId === index && item.citys && item.citys.length){
						item.citys.forEach((sItem,sIndex)=>{
							if(cId === sIndex){
								town = sItem.areas || []
							}
						})
					}
			    });
			    return town;
			},
			//初始化数据
			initFn(){
				if(!this.province.length){
					lotusAddressJson.forEach((item,index)=>{
					   this.province.push(item);
					});
				}
				//已选择省市区，高亮显示对应选择省市区
				const p = this.lotusAddressData.provinceName;
				const c = this.lotusAddressData.cityName;
				const t = this.lotusAddressData.townName;
				//已选省
				if(p){
					this.pChoseIndex = this.getTarIndex(this.province,'province',p);
				}
				//已选市
				if(p&&c){
					const pid = this.getTarId(p,'province');
					this.city = this.getCityArr(pid);
					this.cChoseIndex = this.getTarIndex(this.city,'city',c);
				}
				//已选区
				if(p&&c&&t){
					this.pChoseIndex = this.getTarIndex(this.province,'province',p);
					const pid = this.getTarId(p,'province');
					this.city = this.getCityArr(pid);
					this.cChoseIndex = this.getTarIndex(this.city,'city',c);
					const cid= this.getTarId(c,'city',pid);
					this.town = this.getTownArr(pid,cid);
					this.tChoseIndex = this.getTarIndex(this.town,'area',t);
				}
				//未选省市区
				if(!p&&!c&&!t){
					this.pChoseIndex = -1;
					this.cChoseIndex = -1;
					this.tChoseIndex = -1;
					this.city = [];
					this.town = [];
				}
			},
			//获取已选省市区
			getChosedData(){
				const pid = this.getTarId(this.provinceName,'province');
				this.city = this.getCityArr(pid);
				const cid= this.getTarId(this.cityName,'city',pid);
				this.town = this.getTownArr(pid,cid);
				//已选省市区获取对应index
				if(this.provinceName){
					this.pChoseIndex = this.getTarIndex(this.province,'province',this.provinceName);
					// 获取省code
					this.provinceCode = this.province[this.pChoseIndex].code
				}
				if(this.cityName){
					this.cChoseIndex = this.getTarIndex(this.city,'city',this.cityName);
					// 获取市code
					this.cityCode = this.city[this.cChoseIndex].code
				}
				if(this.townName){
					this.tChoseIndex = this.getTarIndex(this.town,'area',this.townName);
					// 获取区code
					this.townCode = this.town[this.tChoseIndex].code
				}
			},
			//选择省市区交互
			clickPicker(type,index,item){
				//省
				if(type === 0){
					this.pChoseIndex = index;
					this.provinceName = item.province;
					this.cChoseIndex = -1;
					this.tChoseIndex = -1;
					this.cityName = '';
					this.townName = '';
				}
				//市
				if(type ===1){
					this.cChoseIndex = index;
					this.cityName = item.city;
					this.tChoseIndex = -1;
					this.townName = '';
				}
				//区
				if(type === 2){
					this.tChoseIndex = index;
					this.townName = item.area;
				}
				//获取省市区数据
				this.getChosedData();
			},
			//获取已选省市区index
			getTarIndex(arr,key,tarName){
			    let cIndex = 0;
			    arr.forEach((item,index)=>{
			        if(item[key] === tarName){
			            cIndex = index;
			        }
			    });
			    return cIndex;
			}
		},
		computed:{
			checkStatus(){
				let t = null;
				const _this = this;
				if(!_this.visible){
					_this.visible = _this.lotusAddressData.visible;
					//获取省市区
					_this.provinceName = _this.lotusAddressData.provinceName;
					_this.cityName = _this.lotusAddressData.cityName;
					_this.townName = _this.lotusAddressData.townName;
					//生成初始化数据
					_this.initFn();
					t = _this.visible;
				}
				return t;
			}
		}
	}
</script>

<style scoped>
@import "./Winglau14-lotusAddress.css";
</style>
